﻿Partial Public Class WebFormKunde
    Inherits System.Web.UI.Page
    Public _db As New hc_databaseDataContext

    Public Artlist() As String




    Protected Sub KundeIDSession(ByVal ID As Integer)
        Session("KundeID") = ID

    End Sub

    Protected Sub MultivievUebersicht() Handles ViewUebersicht1.Activate



        lblUeberschrift.Text = "Übersicht: Kunden"


        '#######TEST
        Session("Rang") = 2
        Dim pr(,) As Integer = {{5, 2}, {11, 1}}
        Session("Projekte_Rang") = pr

        '#######TEST



        Dim rang As Integer
        rang = Session("Rang")

        'wenn der Nutzer ein PM oder VP ist
        If rang <= 2 Then

            GVKunden.DataSource = From K In _db.Kundes Select K.Name, K.Branche

        End If


        'wenn er  zur zeit Projekte macht

        Dim projekttab As New DataTable

        projekttab.Columns.Add("Name")
        projekttab.Columns.Add("Branche")

        Dim Projekte As Object


        Projekte = Session("Projekte_Rang")




        Dim i As Integer
        For i = 0 To 100
            Try



                Dim proID As Integer = Projekte(i, 0)
                Dim KundeIDVonProjekt As Integer = (From P In _db.Projekts Where P.ID = proID Select P.KundeID).First

                '  Dim arr() = (From K In _db.kundes Where K.ID = KundeIDVonProjekt Select K.Name, K.Branche).ToArray

                Dim row(1) As String

                row(0) = (From K In _db.Kundes Where K.ID = KundeIDVonProjekt Select K.Name).First
                row(1) = (From K In _db.Kundes Where K.ID = KundeIDVonProjekt Select K.Branche).First



                projekttab.Rows.Add(row)


            Catch ex As Exception
                Exit For
            End Try

        Next




        GVKundenO.DataSource = projekttab
        GVKundenO.DataBind()

        Try
            GVKundenO.Columns.Item(1).ItemStyle.Font.Bold = True

        Catch ex As Exception

        End Try


        If GVKundenO.Rows.Count = 0 Then
            lblError.Visible = True
        Else
            lblError.Visible = False


        End If



        GVKunden.DataBind()



    End Sub


    Protected Sub GVKundenSelect() Handles GVKunden.SelectedIndexChanged
        'Dim index As Integer = GVKunden.SelectedIndex
        Dim KName As String = GVKunden.SelectedRow.Cells.Item(1).Text
        Dim KID As Integer = (From K In _db.Kundes Where K.Name = KName Select K.ID).First


        KundeIDSession(KID)
        Dim kundenname = (From K In _db.Kundes Where K.ID = KID Select K.Name).First

        Me.KundeIDSession(KID)
        TreeView1.Nodes.Item(1).Select()
        btnUebersicht_Click()
        'MultiView1.ActiveViewIndex = 1

        'MultiView2.ActiveViewIndex = 0

        GVAnsprech.DataBind()
        GVAnsprech.SelectedIndex = 0
        GVAnsprech_SelectedIndexChanged()
        lblUeberschrift.Text = "Details von " & kundenname

    End Sub

    Protected Sub GVKundenOSelect() Handles GVKundenO.SelectedIndexChanged
        Dim KName As String = GVKundenO.SelectedRow.Cells.Item(1).Text
        Dim KID As Integer = (From K In _db.Kundes Where K.Name = KName Select K.ID).First
        Dim kundenname = (From K In _db.Kundes Where K.ID = KID Select K.Name).First

        KundeIDSession(KID)


        MultiView1.ActiveViewIndex = 1

        MultiView2.ActiveViewIndex = 0

        GVAnsprech.DataBind()
        GVAnsprech.SelectedIndex = 0
        GVAnsprech_SelectedIndexChanged()
        lblUeberschrift.Text = "Details von " & kundenname


    End Sub

    Protected Sub btnUebersicht_Click() Handles TreeView1.SelectedNodeChanged
        Try

            Dim KID As Integer = Session("KundeID")
            Dim kundenname = (From K In _db.Kundes Where K.ID = KID Select K.Name).First


            If TreeView1.SelectedValue = "Übersicht" Then
                MultiView1.ActiveViewIndex = 0
                lblUeberschrift.Text = "Übersicht: Kunden"

            ElseIf TreeView1.SelectedValue = "Details" Then
                MultiView1.ActiveViewIndex = 1

                MultiView2.ActiveViewIndex = 0
                lblUeberschrift.Text = "Details von " & kundenname

            ElseIf TreeView1.SelectedValue = "Ansprechpartner" Then
                MultiView1.ActiveViewIndex = 1

                MultiView2.ActiveViewIndex = 1
                GVAnsprech.SelectedIndex = 0
                GVAnsprech_SelectedIndexChanged()

                lblUeberschrift.Text = "Ansprechpartner von " & kundenname

            ElseIf TreeView1.SelectedValue = "Projekte" Then
                MultiView1.ActiveViewIndex = 1

                MultiView2.ActiveViewIndex = 2
                lblUeberschrift.Text = "Projekte von " & kundenname
                GridView2.DataBind()

            End If


        Catch ex As Exception

        End Try


    End Sub







    Protected Sub btnAnsprechNeu_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAnsprechNeu.Click

        Dim KID As Integer = Session("KundeID")

        Dim ord As New Ansprechpartner With {.Vorname = VornameTextBox.Text, _
                                             .Nachname = NachnameTextBox.Text, _
                                             .Telefonnummer = TelefonnummerTextBox.Text, _
                                             .Fax = FaxTextBox.Text, _
                                             .email = emailTextBox.Text, _
                                             .Kunde_id = KID}

        _db.Ansprechpartners.InsertOnSubmit(ord)

        _db.SubmitChanges()

        GVAnsprech.DataBind()


        VornameTextBox.Text = ""
        NachnameTextBox.Text = ""
        TelefonnummerTextBox.Text = ""
        FaxTextBox.Text = ""
        emailTextBox.Text = ""


    End Sub

    Protected Sub btnKontaktNeu_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnKontaktNeu0.Click

        Dim AID As Integer = Session("AnsprechID")

        Dim ord As New Kundenkontakt With {.ArtKontakt = ArtTextBox0.Text, _
                                           .Betreff = BetreffTextBox0.Text, _
                                           .Datum = DatumTextBox0.Text, _
                                           .AnsprechpartnerID = AID, _
                                           .Inhalt = InhaltTextBox0.Text}



        _db.Kundenkontakts.InsertOnSubmit(ord)

        _db.SubmitChanges()

        GridView1.DataBind()


        ArtTextBox0.Text = ""
        BetreffTextBox0.Text = ""
        DatumTextBox0.Text = ""
        InhaltTextBox0.Text = ""


    End Sub

    Protected Sub GVAnsprech_SelectedIndexChanged() Handles GVAnsprech.SelectedIndexChanged
        Session("AnsprechID") = GVAnsprech.SelectedRow.Cells.Item(0).Text
        GridView1.DataBind()

        lblKundenkontakt.Text = "Kontakte mit " & GVAnsprech.SelectedRow.Cells.Item(1).Text & GVAnsprech.SelectedRow.Cells.Item(2).Text

        'Dim kid = Session("KundeID")
        'Dim ansprechlist = (From AID In _db.Ansprechpartner Where AID.Kunde_id = kid).ToArray

        'Session("AnsprechID") = ansprechlist(GVAnsprech.SelectedIndex)

        'GridView1.DataBind()

        'lblKundenkontakt.Text = "Kontakte mit " & GVAnsprech.SelectedRow.Cells.Item(1).Text & GVAnsprech.SelectedRow.Cells.Item(2).Text

    End Sub

End Class